package com.webull.information.center.carwler.common.util.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;
import com.webull.information.center.carwler.common.util.jsoup.JsoupModel;
import com.webull.information.client.model.News;

/**
 * 路透个股新闻列表
 *
 * @author shimingjun
 * @version 1.0
 * @date 2016年8月30日 下午1:42:50
 * @since JDK 1.8
 */
@Component
public class ReutersTopNewsListUitl {
    private @Autowired
    HttpJsoupClient htmlPrase;
    protected final Logger logger = LogManager.getLogger(getClass());


    public List<ReutersItem> parseNewsList() {
        String url = "http://mobile.reuters.com/";
		Document doc = htmlPrase.parseDocumentFromUrl(url, JsoupModel.Mobile0, true);
        if (doc == null) {
			logger.warn("ReutersTopNewsListUitl parseNewsList parse url:{} ,doc is null!!", url);
            return null;
        }
		List<ReutersItem> result = new ArrayList<>();
		List<ReutersItem> newsList = parseNewsList(doc);
		if (newsList != null && !newsList.isEmpty())
			result.addAll(newsList);

		List<ReutersItem> dealsList = parseDealsList(doc);
		if (dealsList != null && !dealsList.isEmpty())
			result.addAll(dealsList);
		return result;
    }

	public List<ReutersItem> parseBusinessList() {
		String url = "http://mobile.reuters.com/business";
		Document doc = htmlPrase.parseDocumentFromUrl(url, JsoupModel.Mobile0, true);
		if (doc == null) {
			logger.warn("ReutersTopNewsListUitl parseBusinessList parse url:{} ,doc is null!!", url);
			return null;
		}
		List<ReutersItem> newsList = parseNewsList(doc);

		return newsList;
	}

	public List<ReutersItem> parseMarketsList() {
		String url = "http://mobile.reuters.com/finance/markets";
		Document doc = htmlPrase.parseDocumentFromUrl(url, JsoupModel.Mobile0, true);
		if (doc == null) {
			logger.warn("ReutersTopNewsListUitl parseMarketsList parse url:{} ,doc is null!!", url);
			return null;
		}
		List<ReutersItem> newsList = parseNewsList(doc);
		return newsList;
	}

	private ReutersItem parseItem(Element it0) {
		ReutersItem item = new ReutersItem();
		Optional.ofNullable(it0.select("header>h3>a").first()).ifPresent(a0 -> {
			item.setLink(StringUtils.stripToNull(a0.absUrl("href")));
			item.setTitle(StringUtils.stripToNull(a0.html()));
		});

		Optional.ofNullable(it0.select("figure>a>img").first()).ifPresent(a0 -> {
			boolean present = Optional.ofNullable(it0.select("figure.article-image").first()).isPresent();
			if (present) {
				item.setHome(News.HomeType.frontPage);
			}
			item.setMainPic(StringUtils.stripToNull(a0.absUrl("org-src")));

		});
		// final Document document = get(item.getLink());
		final Document document = htmlPrase.parseDocumentFromUrl(item.getLink(), JsoupModel.Mobile0, true);
		if (document == null) {
			logger.warn("ReutersTopNewsListUitl parseNewsList parse2 url:{} ,doc is null!!", item.getLink());
		}
		Optional.ofNullable(document.select("div.ArticleHeader_date_V9eGk").first())
				.map(v -> StringUtils.stripToNull(v.html())).ifPresent(str -> {
					// Fri Jun 16, 2017 | 1:06 AM EDT
					// July 17, 2017 / 12:22 AM / in 33 minutes
					// <div class="ArticleHeader_date_V9eGk" data-reactid="14">July 17, 2017 / 8:22
					// AM / 38 minutes ago</div>
					String edt = "EDT";
					String timz = StringUtils.substringBeforeLast(str, "/");
					timz = StringUtils.replace(timz, "/", " ");
					timz = timz.replaceAll("\\s+", " ").trim();
					Optional.ofNullable(
							UtilDate.parse(timz, Locale.US, TimeZone.getTimeZone(edt), "MMM dd, yyyy hh:mm a"))
							.ifPresent(d2 -> item.setNewsTime(d2));
				}// <div class="timestamp"> Fri Jun 16, 2017 | 1:06 AM EDT</div>
		);
		if (item.isCheck())
			return item;
		return null;
	}

    private List<ReutersItem> parseNewsList(Document doc) {
        List<ReutersItem> result = new ArrayList<>();
        // 美股新闻页面
        Optional.ofNullable(doc.select("section.top-news-module>div>article"))
				.ifPresent(tops -> tops.forEach(top -> {
					ReutersItem item = parseItem(top);
					if (item != null)
						result.add(item);
				}));

        return result;
    }

	private List<ReutersItem> parseDealsList(Document doc) {
		List<ReutersItem> result = new ArrayList<>();

		Elements elements = doc.select("div.module-container");

		for0: for (int i = 0; elements != null && i < elements.size(); i++) {
			Element element = elements.get(i);
			boolean present = Optional.ofNullable(element.select("div>header>h2>span").first())
					.filter(v -> "Deals".equalsIgnoreCase(v.text())).isPresent();
			if (present) {
				Optional.ofNullable(element.select("div>article")).ifPresent(tops -> tops.forEach(top -> {
					ReutersItem item = parseItem(top);
					if (item != null)
						result.add(item);
				}));
				break for0;
			}
		}
		return result;
	}

    public static void main(String[] args) throws IOException {


		String url2 = "http://mobile.reuters.com";
        // url2 =
        // "http://www.reuters.com/finance/stocks/companyNews?symbol=600036.SS";

		Document doc = get(url2);
		List<ReutersItem> items = new ReutersTopNewsListUitl().parseDealsList(doc);
        System.out.println(items);
    }


    private static Document get(String url) {
        Connection connection = Jsoup.connect(url).userAgent(
				"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
				.header("cache-control", "no-cache")
				.header("x-ivanka-platform", "wscn-platform");
        connection.proxy("127.0.0.1", 1080);
        Document doc2 = null;
        try {
            doc2 = connection.timeout(10000).get();
        } catch (IOException e) {
        }
        return doc2;
    }

    public static class ReutersItem {

        private String title;
        private String link;
        private String mainPic;
        private Date newsTime;
		private Integer home = News.HomeType.top;



        private boolean isCheck() {
            return StringUtils.isNoneBlank(title)
                    && StringUtils.isNoneBlank(link)
                    && newsTime != null;
        }

        public Date getNewsTime() {
            return newsTime;
        }

        public void setNewsTime(Date newsTime) {
            this.newsTime = newsTime;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getLink() {
            return link;
        }

        public void setLink(String link) {
            this.link = link;
        }

        public String getMainPic() {
            return mainPic;
        }

        public void setMainPic(String mainPic) {
            this.mainPic = mainPic;
        }

		public Integer getHome() {
			return home;
		}

		public void setHome(Integer home) {
			this.home = home;
		}




    }

}
